// ==UserScript==
// @name				HWM - Artifact durability
// @namespace			figey_hwm_
// @description			Show artefact durability
// @version				0.1.0
// @run-at				document-end
// @noframe
// @include				/^https?:\/\/(www\.)?(heroeswm\.ru|lordswm\.com)\/home\.php.*/
// @include				/^https?:\/\/(www\.)?(heroeswm\.ru|lordswm\.com)\/pl_info\.php.*/
// @include				/^https?:\/\/(www\.)?(heroeswm\.ru|lordswm\.com)\/inventory\.php.*/
// ==/UserScript==

/**
 * Find parent element by tag name
 * @param node base element
 * @param tag parent element type
 * @param count how many parents we must skip
 * @return parent for custom node or null if no parent
 */
function getParentByTagName(node, tag, count) {
	if( typeof node !== 'object' || node.tagName == 'HTML'
		|| typeof tag !== 'string' || tag == ''
		|| typeof count !== 'number' || count <= 0 
	){
		console.error('Invalid call function ' + this.name);
		return null;
	}

	let parent = node.parentNode;
	tag = tag.toUpperCase();

	while (parent.tagName !== "HTML") {
		if (parent.tagName === tag) {
			if( count == 1 ){
				return parent;
			} else {
				count--;
			}
		}
		parent = parent.parentNode;
	}

	if( parent.tagName !== tag ){
		console.error("Failed found parent for:");
		console.error(node);
		return null;
	}

	return parent;
}
getParentByTagName = getParentByTagName.bind(getParentByTagName);

/**
 * Add element with durability status into artifact image
 * @param element artifact image
 * @param durability artefact durability
 */
function addDurabilityInfo(element, durability){
	const span = document.createElement('div');
	span.style = 'position: absolute; z-index: 5; font-size: 14px; font-weight:bold; padding: 0px 2px; border-radius: 0% 0% 50% 0%;';
	span.textContent = durability.current;
	span.style.textShadow = "1px 0 1px #dabd88, 0 1px 1px #dabd88, -1px 0 1px #dabd88, 0 -1px 1px #dabd88, \
	2px 0 2px #dabd88, 0 2px 2px #dabd88, -2px 0 2px #dabd88, 0 -2px 2px #dabd88";
	element.prepend(span);
}

/**
 * Add alert for low durability arts
 * @param element artifact image
 * @param durability artefact durability
 */
function addDurabilityAlert(element, durability){
	if( durability.current == 0 ){
		element.querySelector('img[src*="artifact"]').style.backgroundColor = 'rgba(128,128,128,0.65)';
	} else if( durability.current < 7 && durability.current < durability.max / 2 ){
		element.querySelector('img[src*="artifact"]').style.backgroundColor = 'rgba(255,0,0,' + (0.58 - 0.08*durability.current) + ')';
	}
}

/**
 * Extract durability info for artifact
 * @param element articaft element
 * @return artefact durability, current and maximum
 */
function getDurability(element){
	const dur = element.querySelector('img').title.match(/(\d+)\/(\d+)/);
	if( dur ){
		return { current: dur[1], max: dur[2] };
	} else {
		return null;
	}
}

/**
 * Get artefact list from page
 * @return array with artefact elements
 */
function getArtifacts(){
	const result = Array();
	document.querySelectorAll('div img[title*="Прочность"]').forEach(function(img){
		const parent = getParentByTagName(img, 'div', 1);
		if( !result.includes(parent) ){
			result.push(parent);
		}
	});

	return result;
}

/** 
 * Player info && main page 
 * Inventory(first load)
 */
getArtifacts().forEach(function(art){
	const durability = getDurability(art)
	addDurabilityInfo(art, durability);
	addDurabilityAlert(art, durability);
});

/**
 * Inventory(only after update)
 * NOTE
 * arts_fud			take of
 * arts_fd_ok		wear
 * arts_fd_none		cant wear
 */
if( location.pathname == '/inventory.php' ){
	const template = document.createElement('template');
	[arts_fud, arts_fd_ok, arts_fd_none].forEach(function(artList){
		for( let i = 0; i < artList.length; ++i){
			if( artList[i] ){
				template.innerHTML = artList[i];
				const artNode = template.content.firstChild;
				const art = artNode.querySelector('div.arts_info');
				const durability = getDurability(art);
				addDurabilityInfo(art, durability);
				addDurabilityAlert(art, durability);
				artList[i] = template.innerHTML;
			}
		}
	});
}


